import tkinter as tk
# 线程处理
from threading import Thread
import time
from tkinter import messagebox
# 导入Get_Qwen类·获取Qwen/Qwen2.5-7B-Instruct返回结果
from WEBAPI import Get_Qwen
# 导入NTPClient类·获取精准的东八区时间
from get_ntplib_nowtime import NTPClient
# 导入时辰显示
from GetShiChen import GetShiChen


# 根据NTP服务获取准确的当前时间
# 全局变量·用于存储获取的时间
show_time = ""
time_list = []

# 全局变量
# 信息集合
global hour_info
# 关闭线程
is_stop = True


def get_time_chen_ke():
    global show_time, time_list  # 声明 show_time 和 time_list 为全局变量
    while True:
        try:
            show_time = NTPClient().get_time().strftime("%Y-%m-%dT%H:%M:%S")
            break
        except Exception as e1:
            print("时间:", e1)
    
    # 在 show_time 成功获取后填充 time_list
    time_list.append(show_time[0:4])  # 年
    time_list.append(show_time[5:7])  # 月
    time_list.append(show_time[8:10])  # 日
    time_list.append(show_time[11:13])  # 时
    time_list.append(show_time[14:16])  # 分
def center_window(window):
    """
    将窗口居中显示
    """
    window.update_idletasks()  # 更新窗口的空闲任务，以确保窗口的尺寸已经被计算
    width = window.winfo_width()  # 获取窗口的宽度
    height = window.winfo_height()  # 获取窗口的高度
    x = (window.winfo_screenwidth() // 2) - (width // 2) - 20  # 计算窗口左边缘的x坐标
    y = (window.winfo_screenheight() // 2) - (height // 2)  # 计算窗口上边缘的y坐标
    window.geometry(f'+{x}+{y}')  # 设置窗口的位置


# 创建一个Tk根窗口实例
root = tk.Tk()

# 设置窗体的大小
root.geometry("800x600")

# 调用函数将窗口居中
center_window(root)

# 取消窗口的大小可变性
root.resizable(False, False)
# 窗体设置标题
root.title("AI易经出行")

# 绘制主界面框架

# 背景色-太师青-稳重
main_frame = tk.Frame(root, bg='#527486')
main_frame.pack(fill=tk.BOTH, expand=True)  # 使Frame填充整个窗体

# 分隔-云门色-祥云福佑-鼎运
canvas = tk.Canvas(root, width=700, height=10, bg="#9AC9D9", highlightthickness=0)
canvas.place(x=50, y=20)

# 九宫格-碧城色-优雅与沉静
for i in range(3):
    for j in range(3):
        x1 = 50 + i * 235
        y1 = 40 + j * 115
        x2 = x1 + 235
        y2 = y1 + 115
        canvas = tk.Canvas(root, width=230, height=110, bg="#0D4E77", highlightthickness=0)
        canvas.pack()
        canvas.place(x=x1, y=y1)

# 分隔-云门色-祥云福佑-中福
canvas = tk.Canvas(root, width=700, height=10, bg="#9AC9D9", highlightthickness=0)
canvas.place(x=50, y=390)

# 功能栏-提夫尼蓝-尊贵
canvas = tk.Canvas(root, width=230*3+10, height=150, bg="#418683", highlightthickness=0)
canvas.pack()
canvas.place(x=50, y=410)

# 分隔-云门色-祥云福佑-底蕴
canvas = tk.Canvas(root, width=700, height=10, bg="#9AC9D9", highlightthickness=0)
canvas.place(x=50, y=570)


# 主页面——非必要信息
def show_message():
    """
    主页面——非必要文字信息——可控制是否显示
    :return:
    """
    main_label_t = tk.Label(root, text="抬头见喜", font=('宋体', 9), fg='#FFFFCC', bg='#527486', compound='left')
    main_label_t.place(x=380, y=0)

    main_label_b = tk.Label(root, text="低头遇福", font=('宋体', 9), fg='#FFFFCC', bg='#527486', compound='left')
    main_label_b.place(x=380, y=580)


# 显示主页面——非必要信息
show_message()

# 所有文字列表
top_label = tk.StringVar()
top_label.set("祥云福佑-鼎运")

# 创建一个StringVar对象，用于存储当前时间以及更新当前时间
NTPC_time = tk.StringVar()

# 创建一个Label对象，用于显示当前时间
main_label_NTPC_time = tk.Label(root, text=str(NTPC_time), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77',
                                compound='left',
                                textvariable=NTPC_time)
main_label_NTPC_time.place(x=300, y=160)
# 用于显示农历时间
n_lunar = tk.StringVar()
main_label_n_lunar = tk.Label(root, text=str(n_lunar), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77',
                              compound='left',
                              textvariable=n_lunar)
main_label_n_lunar.place(x=345, y=195)
# 是否是吉日
n_ji = tk.StringVar()
main_label_n_ji = tk.Label(root, text=str(n_ji), font=('宋体', 15), fg='#FFFFCC', bg='#418683',
                           compound='left',
                           textvariable=n_ji,
                           wraplength=400)  # 设置文本的最大宽度为400像素，超过此宽度将自动换行
main_label_n_ji.place(x=320, y=455)

# 用户显示农历时刻
n_shi_chen = tk.StringVar()
main_label_n_shi_chen = tk.Label(root, text=str(n_shi_chen), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77',
                                 compound='left',
                                 textvariable=n_shi_chen)
main_label_n_shi_chen.place(x=340, y=230)
# 八个方向文字
n = tk.StringVar()  # 北
main_label_n = tk.Label(root, text="北", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_n.place(x=390, y=40)
main_label_n_v = tk.Label(root, text=str(n), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                          textvariable=n)
main_label_n_v.place(x=375, y=70)
n_v_info = tk.StringVar()
main_label_n_v_info = tk.Label(root, text=str(n_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                               wraplength=150, justify='left',
                               compound='center',
                               textvariable=n_v_info)
main_label_n_v_info.place(x=335, y=100)

s = tk.StringVar()  # 南
main_label_s = tk.Label(root, text="南", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_s.place(x=390, y=350)
main_label_s_v = tk.Label(root, text=str(s), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                          textvariable=s)
main_label_s_v.place(x=375, y=290)
s_v_info = tk.StringVar()
main_label_s_v_info = tk.Label(root, text=str(s_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                               wraplength=150, justify='left',
                               compound='center',
                               textvariable=s_v_info)
main_label_s_v_info.place(x=335, y=320)

w = tk.StringVar()  # 西
main_label_w = tk.Label(root, text="西", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_w.place(x=50, y=200)
main_label_w_v = tk.Label(root, text=str(w), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                          textvariable=w)
main_label_w_v.place(x=140, y=180)
w_v_info = tk.StringVar()
main_label_w_v_info = tk.Label(root, text=str(w_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                               wraplength=150, justify='left',
                               compound='center',
                               textvariable=w_v_info)
main_label_w_v_info.place(x=90, y=210)

e = tk.StringVar()  # 东
main_label_e = tk.Label(root, text="东", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_e.place(x=720, y=200)
main_label_e_v = tk.Label(root, text=str(e), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                          textvariable=e)
main_label_e_v.place(x=610, y=180)
e_v_info = tk.StringVar()
main_label_e_v_info = tk.Label(root, text=str(e_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                               wraplength=150, justify='left',
                               compound='center',
                               textvariable=e_v_info)
main_label_e_v_info.place(x=560, y=210)

wn = tk.StringVar()  # 西北
main_label_wn = tk.Label(root, text="西北", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_wn.place(x=50, y=40)
main_label_wn_v = tk.Label(root, text=str(wn), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                           textvariable=wn)
main_label_wn_v.place(x=140, y=70)
wn_v_info = tk.StringVar()
main_label_wn_v_info = tk.Label(root, text=str(wn_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                                wraplength=150, justify='left',
                                compound='center',
                                textvariable=wn_v_info)
main_label_wn_v_info.place(x=90, y=100)

en = tk.StringVar()  # 东北
main_label_en = tk.Label(root, text="东北", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_en.place(x=700, y=40)
main_label_en_v = tk.Label(root, text=str(en), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                           textvariable=en)
main_label_en_v.place(x=610, y=70)
en_v_info = tk.StringVar()
main_label_en_v_info = tk.Label(root, text=str(en_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                                wraplength=150, justify='left',
                                compound='center',
                                textvariable=en_v_info)
main_label_en_v_info.place(x=560, y=100)

ws = tk.StringVar()  # 西南
main_label_ws = tk.Label(root, text="西南", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_ws.place(x=50, y=350)
main_label_ws_v = tk.Label(root, text=str(ws), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                           textvariable=ws)
main_label_ws_v.place(x=140, y=290)
ws_v_info = tk.StringVar()
main_label_ws_v_info = tk.Label(root, text=str(ws_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                                wraplength=150, justify='left',
                                compound='center',
                                textvariable=ws_v_info)
main_label_ws_v_info.place(x=90, y=320)

es = tk.StringVar()  # 东南
main_label_es = tk.Label(root, text="东南", font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_label_es.place(x=700, y=350)
main_label_es_v = tk.Label(root, text=str(es), font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77', compound='center',
                           textvariable=es)
main_label_es_v.place(x=610, y=290)
es_v_info = tk.StringVar()
main_label_es_v_info = tk.Label(root, text=str(es_v_info), font=('宋体', 10), fg='#FFFFCC', bg='#0D4E77',
                                wraplength=150, justify='left',
                                compound='center',
                                textvariable=es_v_info)
main_label_es_v_info.place(x=560, y=320)



def start_fun():
    # 获取时间
    get_time_chen_ke()
    NTPC_time.set(show_time)
    # 获取时辰
    chen_ke = GetShiChen.get_shi_chen(int(time_list[3]), int(time_list[4]))
    n_lunar.set(chen_ke)
    # 获取Qwen/Qwen2.5-7B-Instruct返回结果
    all_result = {}
    # 获取main_entry_user_birthday_entry的内容
    find = main_entry_user_birthday.get()

    while True:
        try:
            if find != "例如：姓名：红目香薰":
                all_result = Get_Qwen.get_api(show_time, find)
            else:
                all_result = Get_Qwen.get_api(show_time, "当前时间："+show_time)    
            print(all_result)
            break
        except Exception as e1:
            print("JSON异常:", e1)
    # 今日整体大局
    today_luck = all_result['today_luck']
    n_ji.set(today_luck)
    # 八个方向吉凶
    result = all_result['eight']
    # 北
    n.set(result['北']['jx'])
    if str(result['北']['jx']).find("吉") != -1:
        main_label_n_v.configure(fg='#FFD700')
    else:
        main_label_n_v.configure(fg='black')
    n_v_info.set(result['北']['info'])
    # 南
    s.set(result['南']['jx'])
    if str(result['南']['jx']).find("吉") != -1:
        main_label_s_v.configure(fg='#FFD700')
    else:
        main_label_s_v.configure(fg='black')
    s_v_info.set(result['南']['info'])
    # 西
    w.set(result['西']['jx'])
    if str(result['西']['jx']).find("吉") != -1:
        main_label_w_v.configure(fg='#FFD700')
    else:
        main_label_w_v.configure(fg='black')
    w_v_info.set(result['西']['info'])
    # 东
    e.set(result['东']['jx'])
    if str(result['东']['jx']).find("吉") != -1:
        main_label_e_v.configure(fg='#FFD700')
    else:
        main_label_e_v.configure(fg='black')
    e_v_info.set(result['东']['info'])
    # 西北
    wn.set(result['西北']['jx'])
    if str(result['西北']['jx']).find("吉") != -1:
        main_label_wn_v.configure(fg='#FFD700')
    else:
        main_label_wn_v.configure(fg='black')
    wn_v_info.set(result['西北']['info'])
    # 东北
    en.set(result['东北']['jx'])
    if str(result['东北']['jx']).find("吉") != -1:
        main_label_en_v.configure(fg='#FFD700')
    else:
        main_label_en_v.configure(fg='black')
    en_v_info.set(result['东北']['info'])
    # 西南
    ws.set(result['西南']['jx'])
    if str(result['西南']['jx']).find("吉") != -1:
        main_label_ws_v.configure(fg='#FFD700')
    else:
        main_label_ws_v.configure(fg='black')
    ws_v_info.set(result['西南']['info'])
    # 东南
    es.set(result['东南']['jx'])
    if str(result['东南']['jx']).find("吉") != -1:
        main_label_es_v.configure(fg='#FFD700')
    else:
        main_label_es_v.configure(fg='black')
    es_v_info.set(result['东南']['info'])

thread_update_time = Thread(target=start_fun)
thread_update_time.start()

def flush():
    thread_flush = Thread(target=start_fun)
    thread_flush.start()

# 刷新数据
tk_button_now_luck = tk.Button(root, text="(刷新)", font=('宋体', 10), fg='#FFFFCC', command=flush,
                               bg="#0D4E77")
tk_button_now_luck.place(x=450, y=235)
# 用户出生年月日提示文字
main_label_user_birthday = tk.Label(root, text="可以通过姓名、QQ、邮箱、手机号、生日等信息进行推算。", font=('宋体', 15), fg='#FFFFCC', bg='#418683')
main_label_user_birthday.place(x=55, y=420)
# 用户自定义生活中的易经问题输入组件
main_entry_user_birthday = tk.Entry(root, width=22, font=('宋体', 15), fg='#FFFFCC', bg='#0D4E77')
main_entry_user_birthday.insert(0, '例如：姓名：红目香薰')
main_entry_user_birthday.place(x=53, y=460)
# AI分析按钮
ai_button = tk.Button(root, text="开始分析", width=29, command=flush)
ai_button.place(x=58, y=500)

def on_closing():
    """
    关闭窗口时触发的事件
    :return:
    """
    if tk.messagebox.askokcancel("退出", "您确定要退出吗？"):
        global is_stop
        is_stop = False
        root.destroy()
    exit(0)

# 拦截窗口关闭事件
root.protocol("WM_DELETE_WINDOW", on_closing)

# 启动事件循环
root.mainloop()